package com.xauto.pioneer.appuser.activity.mine.balance;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.adapter.BankAdapter;
import com.xauto.pioneer.appuser.model.Bank;
import com.xauto.pioneer.appuser.model.ConfigVersion;
import com.xauto.pioneer.appuser.view.SideBar;
import com.xauto.pioneer.appuser.view.TopBar;

import net.tsz.afinal.FinalDb;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Ellis.Li on 2017/8/8.
 */

public class BankListActivity extends BaseActivity {
    @Bind(R.id.topBar) TopBar topBar;
    @Bind(R.id.store_list)
    ListView storeList;
    @Bind(R.id.letter_dialog)
    TextView letterDialog;
    @Bind(R.id.sidrbar)
    SideBar sidrbar;
    List<Bank> bankList = new ArrayList<>();
    BankAdapter bankAdapter;
    @Override
    protected int getContentView() {
        return R.layout.act_bank_list;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setText(TopBar.TITLE, "银行列表");
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void initData() {
        FinalDb finalDb = FinalDb.create(mContext);
        bankList = finalDb.findAll(Bank.class);
        bankAdapter = new BankAdapter(BankListActivity.this,R.layout.item_bank);
        storeList.setAdapter(bankAdapter);
        // 根据a-z进行排序源数据
        Collections.sort(bankList, new PinyinComparator());
        bankAdapter.getDataList().addAll(bankList);
        bankAdapter.notifyDataSetChanged();
        //设置右侧触摸监听
        sidrbar.setTextView(letterDialog);
        sidrbar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                //该字母首次出现的位置
                int position = bankAdapter.getPositionForSection(s.charAt(0));
                if(position != -1){
                    storeList.setSelection(position);
                }

            }
        });
        storeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bank bank = bankAdapter.getItem(i);
                Intent intent = new Intent();
                intent.putExtra("bank_name",bank.getBank_name());
                intent.putExtra("bank_id",bank.getBank_id());
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }
    public class PinyinComparator implements Comparator<Bank> {

        public int compare(Bank o1, Bank o2) {
            if (o1.getBank_prefix().equals("@")
                    || o2.getBank_prefix().equals("#")) {
                return -1;
            } else if (o1.getBank_prefix().equals("#")
                    || o2.getBank_prefix().equals("@")) {
                return 1;
            } else {
                return o1.getBank_prefix().compareTo(o2.getBank_prefix());
            }
        }

    }
}
